/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.hotTopic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import co.yixiang.api.ApiResult;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.hotTopic.domain.YxHotTopic;
import co.yixiang.modules.hotTopic.service.YxHotTopicService;
import co.yixiang.modules.hotTopic.service.dto.YxHotTopicDto;
import co.yixiang.modules.hotTopic.service.dto.YxHotTopicPage;
import co.yixiang.modules.hotTopic.service.dto.YxHotTopicQueryCriteria;
import co.yixiang.modules.hotTopic.service.mapper.YxHotTopicMapper;
import co.yixiang.modules.hotTopicLike.domain.YxLike;
import co.yixiang.modules.hotTopicLike.service.YxLikeService;
import co.yixiang.modules.hotTopicLike.service.mapper.YxLikeMapper;
import co.yixiang.modules.pay.zhifubao.EmptyUtils;
import co.yixiang.utils.DateUtils;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2021-08-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxHotTopic")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxHotTopicServiceImpl extends BaseServiceImpl<YxHotTopicMapper, YxHotTopic> implements YxHotTopicService {

    private final IGenerator generator;
    private final YxLikeMapper yxLikeMapper;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxHotTopicQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxHotTopic> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxHotTopicDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxHotTopic> queryAll(YxHotTopicQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxHotTopic.class, criteria));
    }


    @Override
    public void download(List<YxHotTopicDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxHotTopicDto yxHotTopic : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("标题", yxHotTopic.getTitle());
            map.put("内容", yxHotTopic.getContent());
            map.put(" picture",  yxHotTopic.getPicture());
            map.put("评论数量", yxHotTopic.getCommentsNumber());
            map.put("点赞数量", yxHotTopic.getLikeNumber());
            map.put("创建时间", yxHotTopic.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public ApiResult<Page<YxHotTopicPage>> getHotTopicPage(Integer page, Integer limit) {
        Page page1=new Page(page,limit);
        Page<YxHotTopicPage> paList = this.baseMapper.getHotTopicPage(page1);
        return ApiResult.ok(paList);
    }


    @Override
    public ApiResult<Object> getHotTopicDetails(Integer hotTopicId) {
        YxHotTopic yxHotTopic = this.baseMapper.selectById(hotTopicId);
        YxHotTopicDto yxHotTopicDto = new YxHotTopicDto();
        BeanUtil.copyProperties(yxHotTopic,yxHotTopicDto);
        YxLike yxLike = yxLikeMapper.selectOne(new LambdaQueryWrapper<YxLike>().eq(YxLike::getHotTopicId,yxHotTopic.getId()));
        if(EmptyUtils.isNotEmpty(yxLike)){
            yxHotTopicDto.setMark(true);
        }
        yxHotTopicDto.setCreateTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",yxHotTopic.getCreateTime()));
        return ApiResult.ok(yxHotTopicDto);
    }
}
